const baseUrl = 'http://192.168.200.128:6600'
//导入共享数据包
import { store } from '../store/store'

// 上传位置
export const uploadLocation = (data) => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: `${baseUrl}/service/position/uploadPosition`,
      method: 'POST',
      data,
      header: {
        'content-type': 'application/json',
        token: store.token
      },
      success: (res) => {
        if(res.statusCode==401) {
            wx.showToast({
                icon: 'error',
                title: '请先登录！'
              })
            return 
        }
        
          resolve(res)
        },
      fail: (err) => {
        console.log("上传位置失败！")
          reject(err)
        }
    })
  })
}

// 获取其他用户位置
export const getNearbyLocations = (latitude, longitude) => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: `${baseUrl}/service/position/getNearbyUsers`,
      method: 'GET',
      data: { latitude, longitude },
      header: {
        token: store.token
      },
      success: (res) => {
        if(res.statusCode==401) {
            wx.showToast({
                icon: 'error',
                title: '请先登录！'
              })
            return 
        }
          resolve(res)
        },
      fail: (err) => {
          reject(err)
          console.log("查询附近用户失败！")
    }
    })
  })
}